###
###
 # @ : Copyright (c) 2020 Phytium Information Technology, Inc. 
 #  
 # SPDX-License-Identifier: Apache-2.0.
 # 
 # @Date: 2021-06-30 17:24:45
 # @LastEditTime: 2021-09-22 18:08:51
 # @Description:  This files is for 
 # 
 # @Modify History: 
 #  Ver   Who        Date         Changes
 # ----- ------     --------    --------------------------------------

#!/bin/sh
# online install would wget cc and install
# offline install need put cc package under folder 'tools'
if [ "$1" = "-online" ]; then
    OFFLINE_INSTALL=0 
else
    OFFLINE_INSTALL=1
fi

# check install envrionment
PLATFORM_TAGS=("MacOS_i386" "Linux_x86_64" "Linux_aarch64")
MAC_OS_I386=0
LINUX_X86=1
LINUX_AARCH64=2
PLATFORM=
ARCH=$(uname -p)
OS=$(uname -s)
if [ $ARCH == i386 ] && [ $OS == Darwin ]; then
    PLATFORM=$MAC_OS_I386
elif [ $ARCH == x86_64 ] && [ $OS == Linux ]; then
    PLATFORM=$LINUX_X86
elif [ $ARCH == aarch64 ] && [ $OS == Linux ]; then
    PLATFORM=$LINUX_AARCH64
else
    echo "[1]: Platform not Support !!! "
    exit
fi

echo "[1]: Platform: "${PLATFORM_TAGS[PLATFORM]}

# profile to save environment variables
if [ $PLATFORM == MAC_OS_I386 ]; then
    PROFILE_PATH=~/.bash_profile
else
    PROFILE_PATH=~/.profile
fi

echo "[1]: Environment Variables at: "$PROFILE_PATH

if [ ! -f  $PROFILE_PATH ] || [ ! -x $PROFILE_PATH ] ; then
    echo "[1]: Profile not exist or non access rights !!!"
    exit
fi

# get absoulte path of script
export STANDALONE_SDK_ROOT=$(pwd)
echo "[1]: Standalone SDK at "$STANDALONE_SDK_ROOT

INSTALL_PATH=$STANDALONE_SDK_ROOT/tools

# make sure all src file ends with LF
#find . -type f -exec dos2unix {} \;
# make sure sdk scripts are executable

if [ $PLATFORM == MAC_OS_I386 ]; then
    chmod +x $STANDALONE_SDK_ROOT/*.sh
    chmod +x $STANDALONE_SDK_ROOT/scripts/*.sh
    chmod +x $STANDALONE_SDK_ROOT/make/*.mk
    chmod +x $STANDALONE_SDK_ROOT/lib/Kconfiglib/*.py
else
    chmod +x $STANDALONE_SDK_ROOT/*.sh --silent
    chmod +x $STANDALONE_SDK_ROOT/scripts/*.sh --silent
    chmod +x $STANDALONE_SDK_ROOT/make/*.mk --silent
    chmod +x $STANDALONE_SDK_ROOT/lib/Kconfiglib/*.py --silent
fi

if [ $? -ne 0 ]; then
    echo "[3]: Failed when assign file premission !!!"
#    exit
fi

# remove environment variables
if [ $PLATFORM == MAC_OS_I386 ]; then
    sed -i ".bak" '### PHYTIUM STANDALONE SDK SETTING START/d' $PROFILE_PATH
    sed -i ".bak" '/export  AARCH32_CROSS_PATH=/d' $PROFILE_PATH
    sed -i ".bak" '/export  PATH=\$PATH:\$AARCH32_CROSS_PATH/d' $PROFILE_PATH
    sed -i ".bak" '/export  AARCH64_CROSS_PATH=/d' $PROFILE_PATH
    sed -i ".bak" '/export  PATH=\$PATH:\$AARCH64_CROSS_PATH/d' $PROFILE_PATH
    sed -i ".bak" '/export  STANDALONE_SDK_ROOT=/d' $PROFILE_PATH
else
    sed -i '/export AARCH32_CROSS_PATH=/d' $PROFILE_PATH
    sed -i '/export PATH=\$PATH:\$AARCH32_CROSS_PATH/d' $PROFILE_PATH
    sed -i '/export AARCH64_CROSS_PATH=/d' $PROFILE_PATH
    sed -i '/export PATH=\$PATH:\$AARCH64_CROSS_PATH/d' $PROFILE_PATH
    sed -i '/export STANDALONE_SDK_ROOT=/d' $PROFILE_PATH
fi
echo "[2]: Reset Environment"

#####################################Install GCC Tools##############################################

# name of cc
if [ $PLATFORM == $MAC_OS_I386 ]; then
    AARCH32_CC=gcc-arm-none-eabi-10.3-2021.07-mac-10.14.6
    AARCH64_CC=gcc-arm-none-eabi-10.3-2021.07-mac-10.14.6

    # cc package name
    AARCH32_CC_PACK=$AARCH32_CC.tar.bz2
    AARCH64_CC_PACK=$AARCH64_CC.tar.bz2

    # cc package name during download
    AARCH32_CC_DL=$AARCH32_CC.tmp.tar.bz2
    AARCH64_CC_DL=$AARCH64_CC.tmp.tar.bz2  
fi

if [ $PLATFORM == $LINUX_X86 ]; then
    AARCH32_CC=gcc-arm-x86_64-none-eabi-10-2020-q4-major
    AARCH64_CC=gcc-arm-x86_64-aarch64-none-elf-10.2-2020.11

    # url of cc package
    AARCH32_URL=https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/776847/download/gcc-arm-x86_64-none-eabi-10-2020-q4-major.tar.xz
    AARCH64_URL=https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/776846/download/gcc-arm-x86_64-aarch64-none-elf-10.2-2020.11.tar.xz

    # cc package name
    AARCH32_CC_PACK=$AARCH32_CC.tar.xz
    AARCH64_CC_PACK=$AARCH64_CC.tar.xz

    # cc package name during download
    AARCH32_CC_DL=$AARCH32_CC.tmp.tar.xz
    AARCH64_CC_DL=$AARCH64_CC.tmp.tar.xz
fi

if [ $PLATFORM == $LINUX_AARCH64 ]; then
    AARCH32_CC=gcc-arm-aarch64-none-eabi-10-2020-q4-major
    AARCH64_CC=gcc-arm-10.2-2020.11-aarch64-aarch64-none-elf

    AARCH32_URL=https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/779742/download/gcc-arm-aarch64-none-eabi-10-2020-q4-major.tar.xz
    AARCH64_URL=https://gitee.com/phytium_embedded/phytium-standalone-sdk/attach_files/779743/download/gcc-arm-10.2-2020.11-aarch64-aarch64-none-elf.tar.xz    

    # cc package name
    AARCH32_CC_PACK=$AARCH32_CC.tar.xz
    AARCH64_CC_PACK=$AARCH64_CC.tar.xz

    # cc package name during download
    AARCH32_CC_DL=$AARCH32_CC.tmp.tar.xz
    AARCH64_CC_DL=$AARCH64_CC.tmp.tar.xz
fi

echo "[4]: Install CC...."

# cc install path
export AARCH32_CROSS_PATH=$INSTALL_PATH/$AARCH32_CC
export AARCH64_CROSS_PATH=$INSTALL_PATH/$AARCH64_CC

# remove tmp load package 
[ -f $INSTALL_PATH/$AARCH32_CC_DL ] && rm $INSTALL_PATH/$AARCH32_CC_DL
[ -f $INSTALL_PATH/$AARCH64_CC_DL ] && rm $INSTALL_PATH/$AARCH64_CC_DL

# do not download if package exists
# do not download if compiler already installed
if [ $OFFLINE_INSTALL -ne 1 ]; then
    if [ ! -d $AARCH32_CROSS_PATH ] && [ ! -f $INSTALL_PATH/$AARCH32_CC_PACK ]; then
        wget $AARCH32_URL -O $INSTALL_PATH/$AARCH32_CC_DL -c -t 0
# rename downlaod cc package in case download process is intrrupted
        if [ -f $INSTALL_PATH/$AARCH32_CC_DL ]; then
            mv $INSTALL_PATH/$AARCH32_CC_DL $INSTALL_PATH/$AARCH32_CC_PACK
            echo "[4]: Got CC "$INSTALL_PATH/$AARCH32_CC_PACK
        fi
    else
        echo "[4]: AARCH32 CC Already Exists"
    fi
    
    if [ ! -d $AARCH64_CROSS_PATH ] && [ ! -f $INSTALL_PATH/$AARCH64_CC_PACK ]; then 
        wget $AARCH64_URL -O $INSTALL_PATH/$AARCH64_CC_DL -c -t 0
        if [ -f $INSTALL_PATH/$AARCH64_CC_DL ]; then
            mv $INSTALL_PATH/$AARCH64_CC_DL $INSTALL_PATH/$AARCH64_CC_PACK
            echo "[4]: Got CC "$INSTALL_PATH/$AARCH64_CC_PACK
        fi
    else
        echo "[4]: AARCH64 CC Already Exists"
    fi
fi

if [ ! -f $INSTALL_PATH/$AARCH32_CC_PACK ]; then
    echo "[4]: Failed, AARCH32 CC Package Non Found !!!" $INSTALL_PATH "/" $AARCH32_CC_PACK
    exit
fi
if [ ! -f $INSTALL_PATH/$AARCH64_CC_PACK ]; then
    echo "[4]: Failed, AARCH64 CC Package Non Found !!!" $INSTALL_PATH "/" $AARCH64_CC_PACK
    exit    
fi

# untar aarch32 cc
echo "[4]: Untar AARCH32 CC in process.."
if [ ! -d $AARCH32_CROSS_PATH ]; then
    tar -C $INSTALL_PATH/ -xvf $INSTALL_PATH/$AARCH32_CC_PACK
    if [ $? -ne 0 ]; then
        echo "[4]: Failed when Untar AARCH32 CC !!!"
        exit
    fi
fi

# write aarch32 cc path
echo "[4]: AARCH32 CC Install Success at "$AARCH32_CROSS_PATH

# untar aarch64 cc
echo "[4]: Untar AARCH64 CC in process.."
if [ ! -d $AARCH64_CROSS_PATH ]; then
    tar -C $INSTALL_PATH/ -xvf $INSTALL_PATH/$AARCH64_CC_PACK
    if [ $? -ne 0 ]; then
        echo "[4]: Failed when Untar AARCH64 CC !!!"
        exit
    fi
fi

# write aarch64 cc path
echo "[4]: AARCH64 CC Install Success at "$AARCH64_CROSS_PATH

# write environment variables
echo "export AARCH32_CROSS_PATH=$AARCH32_CROSS_PATH" >> $PROFILE_PATH
echo "export PATH=\$PATH:\$AARCH32_CROSS_PATH/bin">> $PROFILE_PATH
echo "export AARCH64_CROSS_PATH=$AARCH64_CROSS_PATH" >> $PROFILE_PATH
echo "export PATH=\$PATH:\$AARCH64_CROSS_PATH/bin">> $PROFILE_PATH
echo "export STANDALONE_SDK_ROOT=$STANDALONE_SDK_ROOT" >> $PROFILE_PATH

if [ $? -ne 0 ]; then
    echo "[4]: Failed when Update Environment Variables !!!"
    exit
fi

# show success info
echo "[5]: Success!!! Standalone SDK is Install at "$STANDALONE_SDK_ROOT
echo "[5]: Phytium Standalone SDK Setup Done!!!"
echo "[5]: Type in ' source " $PROFILE_PATH "' to Active Environment"